package top.abrsm.service.impl;

import top.abrsm.entity.CarEntity;
import top.abrsm.mapper.CarMapper;
import top.abrsm.service.ICarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.abrsm.util.SnowflakeIdWorker;
import top.abrsm.util.StateEnum;
import top.abrsm.util.StateException;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2023-11-18
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, CarEntity> implements ICarService {

    @Resource
    private CarMapper carMapper;

    @Override
    public List<CarEntity> queryList ( CarEntity carEntity ) {
        List<CarEntity> carEntities = carMapper.queryList ( carEntity.getStuId () );
        return carEntities;
    }

    @Override
    public CarEntity saveSingle ( CarEntity carEntity ) {
        carEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        carEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( carEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return carEntity;
    }

    @Override
    public CarEntity deleteSingle ( CarEntity carEntity ) {
        int i = baseMapper.deleteById ( carEntity );
        if ( i == 0 ) {
            throw new StateException ( StateEnum.DELETE_EXCE );
        }
        return carEntity;
    }

}
